package com.ybkj.daijia.server.event.listener.freight;

import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.event.model.DriverPowerOfflineEvent;
import com.ybkj.daijia.server.event.model.freight.FreightOrderFinishEvent;
import com.ybkj.daijia.server.freight.Order;
import com.ybkj.daijia.server.mapper.MemberNoticeMapper;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.SmsTemplet;
import com.ybkj.daijia.server.model.SmsTemplet.SmsType;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.SmsService;
import com.ybkj.daijia.server.service.SmsTempletService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class FreightOrderFinishEventListener implements
    ApplicationListener<FreightOrderFinishEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(FreightOrderFinishEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private MemberNoticeMapper memberNoticeMapper;

    @Autowired
    private MemberService memberService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @Override
    @Async
    public void onApplicationEvent(FreightOrderFinishEvent event) {

        logger.debug("order freight finish event:{}", "");

        Order order = (Order) event.getSource();

        Driver driver = driverService.findOne(order.getEmployId());

        //发送短信
        Company orderCompany = companyService.findOne(order.getCompanyId());
        Company driverCompany = companyService.findOne(driver.getCompanyId());
        Company finalCompany = orderCompany;
        Company main = companyService.getMain();
        Setting setting = settingUtils.get();
        if (!main.getId().equals(orderCompany.getId()) && setting.isSmsMainCompany()) {
            finalCompany = main;
        }

        if (setting.getWhichServicePhone() == 1) {
            main = orderCompany;
        }

        SmsTemplet templet = smsService
            .findByAlias(finalCompany.getId(), SmsType.finishFreightOrder);
        if (null != templet && StringUtils.isNotBlank(templet.getContent())) {
            try {
                smsService.sendSms(order.getPassengerPhone(), "finishFreightOrder",
                    "{\"订单号\":\"" + "***" + order.getOrderNo().substring(8) + "\",\"服务人员工号\":\""
                        + driver.getUsername() + "\",\"等候时间\":\"" + order.getWaitTime() + ""
                        + "\",\"行驶公里\":\"" + order.getMileage() + "" + "\",\"收费金额\":\"" + order
                        .getRealPay().toString() + "\",\"支付方式\":\"" + order.getPreAndAfterPayType()
                        + "\",\"投诉建议电话\":\"" + finalCompany.getTelephone() + "\"}",
                    finalCompany.getId(), 0, finalCompany.getSignName());
            } catch (Exception e) {
                logger.error("发送短信发生异常：{}", e.getMessage());
            }
        }

        //服务人员最低上班余额
        if (driver.getStatus().equals(Driver.DRIVER_ONLINE)
            && driver.getType().equals(Driver.DRIVER_TYPR_PARTTIME)
            && driverCompany.getLowestWorkVirtual().doubleValue() != 0
            && driver.getVirtual().doubleValue() < setting.getLowestWorkVirtual().doubleValue()) {

            driver.setStatus(Driver.DRIVER_OFFLINE);
            driverService.updateStatus(Driver.DRIVER_OFFLINE, driver.getId());

            DriverPowerOfflineEvent driverPowerOfflineEvent = new DriverPowerOfflineEvent(driver,
                Driver.DRIVER_ONLINE, "sys");
            applicationEventMulticaster.multicastEvent(driverPowerOfflineEvent);

        }

    }

}
